<?php
/**
 * polwebgis
 * 
 * This file is part of polwebgis.
 *
 * @copyright   Copyright (c) 2012 Dominik Janisch (http://www.joanneum.at)
 * @lastmodified    $LastChangedDate: 2012-03-07 09:15:12 +0000 (Wed, 07 Mar 2012) $
 * @revision    $Revision: 160 $ 
 */

class LayersGateway extends TableDataGateway
{           
        
    public function __construct()
    {
        $this->establishConnection();
    }
        
    public function __destruct(){}
    
    public function getIntersectRegions($m_id, $layer, $arr_areas)
    {
        $q = 'select a.gid from shape_'.pg_escape_string($m_id).' as a, '.pg_escape_string($layer).' as b where ST_Intersects(a.the_geom, b.the_geom)=true';
        if(count($arr_areas) > 0)
        {
            $q .= ' and b.gid IN ('.implode(",", $arr_areas).')';
        }
        $stmt = $this->database_connection_->newStatement($q);
        $stmt->execute();
        $arr_return = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $arr_return[$row['gid']] = $row['gid'];
        return $arr_return;
    }
    
    public function getContainedOverlayRegions($m_id, $layer, $arr_areas)
    {
        $q = 'select a.gid from shape_'.pg_escape_string($m_id).' as a, '.pg_escape_string($layer).' as b where ST_Contains(a.the_geom, b.the_geom)=true'; 
        if(count($arr_areas) > 0)
        {
            $q .= ' and b.gid IN ('.implode(",", $arr_areas).')';
        }
        $stmt = $this->database_connection_->newStatement($q);
        $stmt->execute();
        $arr_return = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $arr_return[$row['gid']] = $row['gid'];
        return $arr_return;
    }
    
    public function getContainedBaseLayerRegions($m_id, $layer, $arr_areas)
    {
        $q = 'select a.gid from shape_'.pg_escape_string($m_id).' as a, '.pg_escape_string($layer).' as b where ST_Contains(b.the_geom, a.the_geom)=true';
        if(count($arr_areas) > 0)
        {
            $q .= ' and b.gid IN ('.implode(",", $arr_areas).')';
        }
        $stmt = $this->database_connection_->newStatement($q);
        $stmt->execute();
        $arr_return = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $arr_return[$row['gid']] = $row['gid'];
        return $arr_return;
    }
    
    public function getWithinRegions($m_id, $layer, $distance, $arr_areas)
    {
        $q = 'select a.gid from shape_'.pg_escape_string($m_id).' as a, '.pg_escape_string($layer).' as b where ST_DWithin(b.the_geom, a.the_geom, '.pg_escape_string((float)$distance).')';
        if(count($arr_areas) > 0)
        {
            $q .= ' and b.gid IN ('.implode(",", $arr_areas).')';
        }
        $stmt = $this->database_connection_->newStatement($q);
        $stmt->execute();
        $arr_return = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $arr_return[$row['gid']] = $row['gid'];
        return $arr_return;
    }
    
    public function getRegions($layer, $is_shape)
    {
        if($is_shape)
        {
            $stmt = $this->database_connection_->newStatement('select gid, region from '.pg_escape_string($layer).' order by region');
        }
        else
        {
            $stmt = $this->database_connection_->newStatement('select gid, overlay_name as region from '.pg_escape_string($layer).' order by overlay_name');
        }
        $stmt->execute();
        $arr_return = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $arr_return[$row['gid']] = $row['region'];
        return $arr_return;
    }
}

?>
